package com.lzx.iteam.skin;

import android.content.Context;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * Created by hfx on 15-1-4.
 */
public class UnZip {

    private Context mContext;

    public UnZip(Context context) {
        mContext = context;
        checkSkinDir();
    }

    public void checkSkinDir() {
        File dir = mContext.getFilesDir();
        File key = new File(dir.getAbsolutePath() + "/" + "skin");
        if (!key.exists() && !key.isDirectory()) {
            key.mkdir();
        }
    }

    public void UnZipFolder(String zipFileString, String outPathString)
            throws Exception {
        ZipInputStream inZip = new ZipInputStream(new FileInputStream(
                zipFileString));
        ZipEntry zipEntry;
        String szName = "";

        while ((zipEntry = inZip.getNextEntry()) != null) {
            szName = zipEntry.getName();

            if (zipEntry.isDirectory()) {

                // get the folder name of the widget
                szName = szName.substring(0, szName.length() - 1);
                File folder = new File(outPathString + File.separator + szName);
                if (!folder.exists()) {
                    folder.mkdirs();
                }
            } else {

                File file = new File(outPathString + File.separator + szName);
                String surePath = file.getParent();
                File sureFile = new File(surePath);
                if (!sureFile.exists()) {
                    sureFile.mkdir();
                }
                file.createNewFile();
                // get the output stream of the file
                FileOutputStream out = new FileOutputStream(file);
                int len;
                byte[] buffer = new byte[1024];
                // read (len) bytes into buffer
                while ((len = inZip.read(buffer)) != -1) {
                    // write (len) byte from buffer at the position 0
                    out.write(buffer, 0, len);
                    out.flush();
                }
                out.close();
            }
        }// end of while
        deleteUnUesless(mContext);
        inZip.close();
    }

    public void deleteUnUesless(Context context) {
        File file = context.getFilesDir();
        File skinFold = new File(file.getAbsolutePath() + File.separator + "skin");
        if (skinFold.exists()) {
            ArrayList<String> fileList = readFile(skinFold);
            for (String filePath : fileList) {
                if (filePath.endsWith("__MACOSX")) {
                    File unless = new File(filePath);
                    if (unless.exists()) {
                        unless.delete();
                    }
                }
            }
        }
    }

    public void deleteDirByDir(String skinPath){
        File skinFold = new File(skinPath);
        if(skinFold.exists()){
            deleteFile(skinFold);
        }
    }

    public void deleteFile(File file) {
        if (file.exists() == false) {
            return;
        } else {
            if (file.isFile()) {
                file.delete();
                return;
            }
            if (file.isDirectory()) {
                File[] childFile = file.listFiles();
                if (childFile == null || childFile.length == 0) {
                    file.delete();
                    return;
                }
                for (File f : childFile) {
                    deleteFile(f);
                }
                file.delete();
            }
        }
    }

    public ArrayList<String> readFile(File file) {
        ArrayList<String> list = new ArrayList<String>();
        File[] files = file.listFiles();
        for (File f : files) {
            if (!f.getName().equals("__MACOSX")) {
                if (!f.getName().equals(".DS_Store")) {
                    list.add(f.getAbsolutePath());
                }
            }
        }
        Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
        return list;
    }
}
